#include "stdafx.h"
#include "simple_unit_test.h"

using namespace lfq;

template <typename Allocator> struct TestByAllocator
{
	static void push_and_pop_all()
	{
		typedef lockfree_queue<int, Allocator> IntQueue;

		IntQueue q;

		q.push(1);
		q.push(2);
		q.push(3);

		IntQueue::unsafe_list popList;

		q.pop_all(popList);

		int i = 1;
		for (auto it = popList.begin(); it != popList.end(); ++it)
		{
			assert(i == *it);
			++i;
		}
	}

	static void splice()
	{
		typedef lockfree_queue<int, Allocator> IntQueue;
		IntQueue q;

		IntQueue::unsafe_list l;

		l.push_back(1);
		l.push_back(2);

		q.splice(l);

		IntQueue::unsafe_list rst;
		q.pop_all(rst);

		auto it = rst.begin();

		ASSERT_EQ(1, *it);
		++it;
		ASSERT_EQ(2, *it);
	}

	static void run_all()
	{
		push_and_pop_all();
		splice();
	}
};

TEST_F(basic_allocator)
{
	TestByAllocator<allocator::entry_allocator_default<int>>::run_all();
}

TEST_F(tls_allocator)
{
	TestByAllocator<allocator::entry_allocator_tls_chunk<int>>::run_all();
}

TEST_F(tls_reuse_allocator)
{
	TestByAllocator<allocator::entry_allocator_tls_chunk_reuse<int>>::run_all();
}

